﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPDI.DrawingStandard
{
    public class TextPloter
    {
        /// <summary>
        /// 带比例尺标题
        /// </summary>
        /// <param name="db"></param>
        /// <param name="Point">插入点坐标</param>
        /// <param name="text">标题文本</param>
        /// <param name="scaleString">比例尺文本</param>
        /// <param name="ext"></param>
        /// <param name="titleFontSize">标题文本大小</param>
        /// <param name="scaleFontSize">比例尺文本大小</param>
        /// <param name="_scale">缩放比例</param>
        /// <param name="tstyle">文字样式</param>
        /// <param name="isModel">是否存入模型空间</param>
        /// <returns>返回DBObjectCollection数据集，1：标题文本；2：上横线；3：下横线；4：比例尺文本</returns>
        public static DBObjectCollection AddTitle(Point2d insertPt, string text, string scaleString, 
            double titleFontSize = 4.0, double scaleFontSize = 2.5, double _scale = 1, string tstyle = "H仿宋")
        {
            try
            {
                DBObjectCollection res = new DBObjectCollection();
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    DBText Title = new DBText()
                    {
                        Height = titleFontSize * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = st[tstyle].GetXScale(),
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);
                    res.Add(Title);

                    Polyline line = new Polyline() { Closed = false, Layer = "H文本" };//定义不封闭的Polyline 平面虚线
                    line.AddVertexAt(0, insertPt.C2D(-width / 2 - 0.5 * _scale, 1 * _scale), 0, 0.25 * _scale, 0.25 * _scale);
                    line.AddVertexAt(1, insertPt.C2D(width / 2 + 0.5 * _scale, 1 * _scale), 0, 0.25 * _scale, 0.25 * _scale);
                    res.Add(line);

                    Polyline line1 = new Polyline() { Closed = false, Layer = "H文本" };//定义不封闭的Polyline 平面虚线
                    line1.AddVertexAt(0, insertPt.C2D(-width / 2 - 0.5 * _scale, 0), 0, 0, 0);
                    line1.AddVertexAt(1, insertPt.C2D(width / 2 + 0.5 * _scale, 0), 0, 0, 0);
                    res.Add(line1);

                    if (!string.IsNullOrEmpty(scaleString))
                    {
                        DBText num = new DBText();
                        num.TextString = scaleString;
                        num.Height = scaleFontSize * _scale;
                        num.Position = insertPt.C3D(width / 2 + 1 * _scale, 1 * _scale);

                        num.HorizontalMode = TextHorizontalMode.TextLeft;
                        num.VerticalMode = TextVerticalMode.TextVerticalMid;
                        num.AlignmentPoint = num.Position;

                        num.TextStyleId = st[tstyle];
                        num.WidthFactor = st[tstyle].GetXScale();
                        num.Layer = "H文本";
                        res.Add(num);
                    }
                }
                return res;
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 绘制文字(DB)
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="_scale"></param>
        /// <param name="height"></param>
        /// <param name="tstyle"></param>
        /// <param name="Rot_rad"></param>
        /// <returns></returns>
        public static DBObjectCollection AddDBText( Point3d insertPt, string text, double _scale,
            double height=2.5, string tstyle = "H仿宋", double Rot_rad = 0,
            TextHorizontalMode hm=TextHorizontalMode.TextLeft,TextVerticalMode vm=TextVerticalMode.TextBottom)
        {
            try
            {
                Database db = HostApplicationServices.WorkingDatabase;
                DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    //DBText
                    DBText txt = new DBText();
                    txt.Height = height * _scale;
                    txt.TextString = text;
                    txt.TextStyleId = st[tstyle];
                    txt.Position = insertPt;
                    txt.Rotation = Rot_rad;
                    txt.WidthFactor = st[tstyle].GetXScale();
                    txt.Layer = "H文本";
                    txt.HorizontalMode = hm;
                    txt.VerticalMode = vm;
                    txt.AlignmentPoint = insertPt;

                    res.Add(txt);
                    return res;
                }
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }


        /// <summary>
        /// 从文件读取设计说明，生成多行文本，左上角点插入。
        /// </summary>
        /// <param name="LeftUpRefPoint"></param>
        /// <param name="FilePath"></param>
        /// <param name="width">文本宽度</param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static DBObjectCollection AddNoteFromFile(Point3d LeftUpRefPoint,string FilePath, double width, double height = 3.5, string tstyle = "H仿宋")
        {
            DBObjectCollection res = new DBObjectCollection();
            Database db = HostApplicationServices.WorkingDatabase;
            List<string> content=new List<string>();
            using (StreamReader sr = new StreamReader(FilePath))
            {
                string line;             
                while ((line = sr.ReadLine()) != null)
                {
                    content.Add(line.Trim());
                }
            }
            var text="\\pxi-1,l1,qj;注：\\P"+string.Join("\\P", content);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                MText Mtext = new MText()
                {
                    TextHeight = height,
                    Contents = text,
                    TextStyleId = st[tstyle],
                    Location = LeftUpRefPoint,
                    Layer = "H文本",
                    LineSpacingFactor = 1.0,
                    LineSpacingStyle = LineSpacingStyle.Exactly,
                    Width = width,
                    
                };

                res.Add(Mtext);
            }
            return res;
        }

        /// <summary>
        /// 绘制多行文字
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="_scale"></param>
        /// <param name="height">字体高度</param>
        /// <param name="strWidth">字符串宽度-1：不限宽度 ；>0：多行文本字符串宽度不能超过设置值</param>
        /// <param name="tstyle"></param>
        /// <param name="Rotation"></param>
        /// <returns></returns>
        public static DBObjectCollection AddMText(Point3d insertPt, string text, double _scale,double height = 2, 
            double strWidth = -1, string tstyle = "H仿宋", double Rotation = 0)
        {
            try
            {
                Database db = HostApplicationServices.WorkingDatabase;
                DBObjectCollection res = new DBObjectCollection();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    MText Mtext = new MText();
                    Mtext.TextHeight = height * _scale;
                    Mtext.Contents = text;
                    Mtext.TextStyleId = st[tstyle];
                    Mtext.Location = insertPt;
                    Mtext.Layer = "H文本";
                    Mtext.Rotation = Rotation;
                    Mtext.LineSpaceDistance = (height+0.1) * _scale;
                    if (strWidth > 0)
                    {
                        Mtext.Width = strWidth;
                    }

                    res.Add(Mtext);
                    return res;
                }
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 绘制带下划线的文本
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Point"></param>
        /// <param name="text"></param>
        /// <param name="_scale"></param>
        /// <param name="height"></param>
        /// <param name="tstyle"></param>
        /// <param name="Rotation"></param>
        /// <returns>返回DBObjectCollection数据集，1：标题文本；2：横线</returns>
        public static DBObjectCollection AddTitleUnderline(Point2d insertPt, string text,double _scale, 
            double height = 2, string tstyle = "H仿宋", double Rotation = 0)
        {
            try
            {
                Database db = HostApplicationServices.WorkingDatabase;
                DBObjectCollection res = new DBObjectCollection();
            
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    DBText Title = new DBText()
                    {
                        Height = height * _scale,
                        TextStyleId = st[tstyle],
                        TextString = text,
                        WidthFactor = st[tstyle].GetXScale(),
                        HorizontalMode = TextHorizontalMode.TextCenter,
                        VerticalMode = TextVerticalMode.TextBase,
                        AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                    };

                    double width = Title.GetWidth();
                    Title.Position = insertPt.C3D(-width / 2, Title.GetHeight() + 2 * _scale);

                    Polyline line1 = new Polyline() { Closed = false, Layer = "H文本" };//定义不封闭的Polyline 平面虚线
                    line1.AddVertexAt(0, insertPt.C2D(-width / 2, 0), 0, 0, 0);
                    line1.AddVertexAt(1, insertPt.C2D(width / 2, 0), 0, 0, 0);

                    res.Add(Title);
                    res.Add(line1);
                }
                return res;
            }
            catch(System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
    }
}

